<!-- 搜索项目 -->
<template>
  <h-select-input-popup
    ref="hSelectInputPopup"
    valKey="empId"
    v-model="val"
    v-bind="$attrs"
    v-on="$listeners"
    :params="queryParams"
    :keywords.sync="keywords"
		searchPlaceholder="请输入用户姓名"
    :getList="getList"
    @getDetail="getDetail"
		:isMulti="muitFlag"
  >
    <template v-slot:default="{ item }">
      <view class="project-item">
        <view class="option">
          <view class="lf">用户账号</view>
          <view class="rl">{{ item.username }}</view>
        </view>
        <view class="option">
          <view class="lf">用户姓名</view>
          <view class="rl">{{ item.realname }}</view>
        </view>
        <view class="option">
          <view class="lf">性别</view>
          <view class="rl">{{ item.sex_dictText }}</view>
        </view>
        <view class="option">
          <view class="lf">手机</view>
          <view class="rl">{{ item.phone }}</view>
        </view>
        <view class="option">
          <view class="lf">部门</view>
          <view class="rl">{{ item.orgCodeTxt }}</view>
        </view>
      </view>
    </template>
  </h-select-input-popup>
</template>

<script>
import { erpmngUserList, erpmngUserId, erpmngUserIds } from '@/api/index.js'

export default {
  name: 'HSelectPersonInputPopup',
  data() {
    return {
      getList: erpmngUserList,
      params: {
        column: 'createTime',
        order: 'desc',
        pageNo: 1,
        pageSize: 10,
      },
      keywords: '',
    }
  },
  model: {
    prop: 'value',
    event: 'input',
  },
  props: {
    value: {
      type: [String, Number, Array],
      default: null,
    },
    muitFlag: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    val: {
      get() {
        return this.value
      },
      set() {
        this.$emit('input', this.value)
      },
    },
    queryParams() {
      const params = {
        ...this.params,
      }
      if (this.keywords) {
        params.realname = this.keywords
      }
      return params
    },
  },
  methods: {
    // 获取详情
    getDetail(value) {
      if (this.muitFlag || value.split(',').length>0) {
        erpmngUserIds({ empId: value }).then((res) => {
          if (res.success) {
            this.$refs.hSelectInputPopup.title = res.result.map((i) => i.realname).join(',')
            this.$emit('select', {
              data: res.result,
              id: value,
            })
          }
        })
        return
      } else {
        erpmngUserId({ empId: value }).then((res) => {
          if (res.success) {
            this.$refs.hSelectInputPopup.title = res.result.realname
            this.$emit('select', {
              data: res.result,
              id: value,
            })
          }
        })
      }
    },
  },
}
</script>

<style lang="scss" scoped>
// 按钮
.btn {
  position: fixed;
  bottom: 20rpx;
  width: 95%;
}
</style>
